﻿package net.wangit.jdbc.sqltype;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.wangit.jdbc.util.SqlDateUtil;


public class TimestampSqlType extends NullableSqlType implements VersionSqlType {

	private static final long serialVersionUID = 1L;

	public TimestampSqlType() {
	}

	public String get(ResultSet rs, String name) throws SQLException {
		Object temp = rs.getObject(name);
		if (temp == null) {
			return null;
		} else {
			String value = temp.toString();
			return value.endsWith(".0") ? value
					.substring(0, value.length() - 2) : value;
		}
	}

	public void setNotNull(PreparedStatement st, String value, int index)
			throws SQLException {
		if ("".equals(value))
			st.setNull(index, sqlType());
		else
			st.setTimestamp(index, SqlDateUtil.getSqlTimestamp(value));
	}

	public int sqlType() {
		return Types.TIMESTAMP;
	}

	public String seed() {
		return Long.toString(System.currentTimeMillis());
	}

	public String next(String current) {
		return seed();
	}
}
